//
//  EPMEmotionBottomView.m
//  WeiBoByOC
//
//  Created by 马继鵬 on 17/4/7.
//  Copyright © 2017年 7TH. All rights reserved.
//

#import "EPMEmotionBottomView.h"

@interface EPMEmotionBottomView()
@property (weak,nonatomic)  UIButton* selectedBtn;
@property (strong,nonatomic) NSMutableArray<UIButton*>* btnList;
@end

@implementation EPMEmotionBottomView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]){
        [self setupUI];
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}

- (NSMutableArray<UIButton*>*)btnList{
    if (_btnList == nil){
        _btnList = [NSMutableArray<UIButton*> new];
    }
    return _btnList;
}

#pragma
#pragma-设置界面
- (void)setupUI{
    self.axis = UILayoutConstraintAxisHorizontal;
    self.distribution = UIStackViewDistributionFillEqually;
    
    
    [self addBtnWithTitle:@"最近" imgName:@"left" type:recent];
    [self addBtnWithTitle:@"默认" imgName:@"mid" type:consent];
    [self addBtnWithTitle:@"emji" imgName:@"mid" type:emoji];
    [self addBtnWithTitle:@"浪小花" imgName:@"right" type:lxh];
    
    
}

- (UIButton*)addBtnWithTitle:(NSString*) title  imgName:(NSString*)imgName type:(BtnType) type{
    UIButton* btn = [[UIButton alloc]init];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"compose_emotion_table_%@_normal",imgName]] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"compose_emotion_table_%@_highlighted",imgName]] forState:UIControlStateSelected];
    btn.tag = type;
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.btnList addObject:btn];
    if (type == consent){
        btn.selected = YES;
        self.selectedBtn = btn;
    }
    [self addArrangedSubview:btn];
    return btn;
}

#pragma
#pragma-点击方法实现
- (void)btnClick:(UIButton*)sender{
    
    if (self.selectedBtn != sender){
        self.selectedBtn.selected = NO;
        sender.selected = YES;
        self.selectedBtn = sender;
    }
    if ( self.btnClickBlock){
        self.btnClickBlock(sender);
    }
    
    
}

- (void) setCurrentBtn:(NSInteger)type
{
    for (UIButton * btn in self.btnList){
        if (btn.tag == type+2000){
            self.selectedBtn.selected = NO;
            btn.selected = YES;
            self.selectedBtn = btn;
            return;
        }
    }
}
@end
